import { reroute } from "../navigations/reroute";
import { BOOTSTRAPPING, LOADING_SOURCE_CODE, MOUNTED, NOT_BOOTSTRAPPED, NOT_LOADED, NOT_MOUNTED, shouldActive } from "./app.helpers";
const apps = [];
export function registerApplication(appName, loadApp, activeTime, customProps){
    apps.push({
        name:appName,
        loadApp,
        activeTime,
        customProps
    })

    reroute(); //开始加载

    /*
        需要拿到 Vue / React 的生命周期
    */
}

export function getAppChanges(){
    const appsNeedUnMount = [] //卸载
    const appsNeedLoad = []    //加载
    const appsNeedMount = []   //挂载

    apps.forEach(app => {
        const appShouldActive = shouldActive(app);
        switch(app.status){
            case NOT_LOADED:
            case LOADING_SOURCE_CODE:
                // 案例： 路由匹配的时候
                if(appShouldActive) appsNeedLoad.push(app);
                break;
            case NOT_BOOTSTRAPPED:
            case BOOTSTRAPPING:
            case NOT_MOUNTED:
                if(appShouldActive) appsNeedMount.push(app);
                break;
            case MOUNTED:
                if(!appShouldActive) appsNeedUnMount.push(app)
        }
    })

    return {
        appsNeedLoad,
        appsNeedMount,
        appsNeedUnMount
    }
}